package org.jeecg.modules.doctor;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterAccount;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterHospitalVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterOrderVo;
import org.jeecg.common.modules.doctor.vo.DoctorRegisterVo;
import org.jeecg.common.modules.doctor.vo.DoctorReqVo;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.common.modules.doctor.entity.DoctorRegisterHospital;
import org.jeecg.common.modules.doctor.service.IDoctorRegisterHospitalService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.data.service.CommonService;
import org.jeecg.modules.data.service.PageReturn;
import org.jeecg.modules.system.vo.ResultVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: doctor_register_hospital
 * @Author: jeecg-boot
 * @Date:   2020-12-18
 * @Version: V1.0
 */
@Api(tags="doctor_register_hospital")
@RestController
@RequestMapping("/doctorRegisterHospital/doctorRegisterHospital")
@Slf4j
public class DoctorRegisterHospitalController extends JeecgController<DoctorRegisterHospital, IDoctorRegisterHospitalService> {
	@Autowired
	private IDoctorRegisterHospitalService doctorRegisterHospitalService;

	 @Resource
	 private RedisUtil redisUtil;
	/**
	 * 分页列表查询
	 *
	 * @param doctorRegisterHospital
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "doctor_register_hospital-分页列表查询")
	@ApiOperation(value="doctor_register_hospital-分页列表查询", notes="doctor_register_hospital-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(DoctorRegisterHospital doctorRegisterHospital,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//分页数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//（1）先查询所有信息
		List<DoctorRegisterHospitalVo> doctorRegisterHospitals = doctorRegisterHospitalService.getInfoList(dataStart,pageSize);
		log.info("查询到的医院数据：{}",String.valueOf(doctorRegisterHospitals));
		//（2）获取总条数
		Integer count = doctorRegisterHospitalService.getInfoListCount();
		log.info("查询到的医院总条数：{}",String.valueOf(count));
		//（3）组织分页的返回数据
		PageReturn pageReturn = new PageReturn();
		ResultVo vo = pageReturn.initResultVo(doctorRegisterHospitals,count,pageNo,pageSize);
		return Result.ok(vo);
	}

	 /**
	  * 医院下的对账单
	  */
	 @GetMapping(value = "/order")
	 public Result<?> hospitalOrder(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 //分页数据开始的记录数
		 int dataStart = (pageNo - 1) * pageSize;

		 //（1）先查询所有信息
		 String hospitalId = req.getParameter("hospitalId");
		 String recordId = req.getParameter("recordId");
		 List<DoctorRegisterOrderVo> doctorRegisterAccounts = doctorRegisterHospitalService.getHospitalOrder(hospitalId,recordId,dataStart,pageSize);
		 log.info("查询到的数据：{}",String.valueOf(doctorRegisterAccounts));

		 //（2）获取总条数
		 Integer count = doctorRegisterHospitalService.getHospitalOrderCount(hospitalId,recordId);

		 //（3）组织分页的返回数据
		 PageReturn pageReturn = new PageReturn();
		 ResultVo vo = pageReturn.initResultVo(doctorRegisterAccounts,count,pageNo,pageSize);
		 return Result.ok(vo);
	 }
	
	/**
	 *   添加
	 *
	 * @param doctorRegisterHospital
	 * @return
	 */
	@AutoLog(value = "doctor_register_hospital-添加")
	@ApiOperation(value="doctor_register_hospital-添加", notes="doctor_register_hospital-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody DoctorRegisterHospital doctorRegisterHospital) {
		Object name = redisUtil.get("name");
		log.info("name是+++++++++++：\n{}",String.valueOf(name));

		CommonService commonService = new CommonService();
		doctorRegisterHospital.setCreateTime(new Date());
		doctorRegisterHospital.setCreateUser(commonService.manageName());
//		doctorRegisterHospital.setUpdateTime(new Date());
//		doctorRegisterHospital.setUpdateUser(commonService.manageName());
		boolean res = doctorRegisterHospitalService.save(doctorRegisterHospital);
		if(res){
			return Result.ok("保存成功！");
		}else{
			return Result.error("保存失败！");
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param doctorRegisterHospital
	 * @return
	 */
	@AutoLog(value = "doctor_register_hospital-编辑")
	@ApiOperation(value="doctor_register_hospital-编辑", notes="doctor_register_hospital-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody DoctorRegisterHospital doctorRegisterHospital) {
		CommonService commonService = new CommonService();

		doctorRegisterHospital.setCreateTime(doctorRegisterHospitalService.getById(doctorRegisterHospital.getId()).getCreateTime());
		doctorRegisterHospital.setUpdateTime(new Date());
		doctorRegisterHospital.setUpdateUser(commonService.manageName());
		log.info("编辑状态：{}",String.valueOf(doctorRegisterHospital));
		boolean res = doctorRegisterHospitalService.updateById(doctorRegisterHospital);
		if(res){
			return Result.ok("保存成功！");
		}else{
			return Result.error("保存失败！");
		}
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_register_hospital-通过id删除")
	@ApiOperation(value="doctor_register_hospital-通过id删除", notes="doctor_register_hospital-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		doctorRegisterHospitalService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "doctor_register_hospital-批量删除")
	@ApiOperation(value="doctor_register_hospital-批量删除", notes="doctor_register_hospital-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.doctorRegisterHospitalService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doctor_register_hospital-通过id查询")
	@ApiOperation(value="doctor_register_hospital-通过id查询", notes="doctor_register_hospital-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		DoctorRegisterHospital doctorRegisterHospital = doctorRegisterHospitalService.getById(id);
		if(doctorRegisterHospital==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(doctorRegisterHospital);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param doctorRegisterHospital
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DoctorRegisterHospital doctorRegisterHospital) {
        return super.exportXls(request, doctorRegisterHospital, DoctorRegisterHospital.class, "doctor_register_hospital");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DoctorRegisterHospital.class);
    }

}
